package com.xxdh.service.impl;

import com.xxdh.Enum.WorkCompleteStatus;
import com.xxdh.domain.po.StudentWorkRelation;
import com.xxdh.domain.vo.WorkTeacherVo;
import com.xxdh.mapper.StudentWorkRelationMapper;
import com.xxdh.service.IStudentWorkRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生作业关系表 服务实现类
 * </p>
 *
 * @author zwg
 * @since 2024-10-19
 */
@Service
public class StudentWorkRelationServiceImpl extends ServiceImpl<StudentWorkRelationMapper, StudentWorkRelation> implements IStudentWorkRelationService {

    @Override
    public void addStudentWorkRelationByStudentIdsAndWorkId(List<Long> studentIds, Long workId) {
        List<StudentWorkRelation> studentWorkRelations = studentIds.stream()
                .map(studentId -> StudentWorkRelation.builder().workId(workId).studentId(studentId).build())
                .collect(Collectors.toList());
        saveBatch(studentWorkRelations);
    }

    /**
     * 根据学生id获取未完成的作业id
     * @param studentId 学生id
     * @return
     */
    @Override
    public List<Long> getNotCompletedWorkIdsByStudentId(Long studentId) {
        // 查询学生未完成的作业id
        List<StudentWorkRelation> studentWorkRelations = lambdaQuery().
                eq(StudentWorkRelation::getStudentId, studentId).
                eq(StudentWorkRelation::getIsComplete, WorkCompleteStatus.NOT_COMPLETE.getStatus()).list();
        List<Long> workIds = studentWorkRelations.stream().map(StudentWorkRelation::getWorkId).collect(Collectors.toList());
        return workIds;
    }

    /**
     * 设置作业总数和完成总数
     * @param workTeacherVo 作业教师视图对象
     */
    @Override
    public void setWorkSumAndCompleteSum(WorkTeacherVo workTeacherVo) {
        // 查询作业的学生数量和完成数量
        List<StudentWorkRelation> studentWorkRelations = lambdaQuery().eq(StudentWorkRelation::getWorkId, workTeacherVo.getId()).list();
        // 计算完成数量
        int completeSum = studentWorkRelations.stream().mapToInt(s -> s.getIsComplete().equals(WorkCompleteStatus.COMPLETE.getStatus()) ? 1 : 0).sum();
        // 设置作业总数和完成总数
        workTeacherVo.setSum(studentWorkRelations.size());
        workTeacherVo.setComplete(completeSum);
    }

    /**
     * 更新作业状态
     * @param studentId 学生id
     * @param workId 作业id
     * @param fileAddress 文件地址
     */
    @Override
    public void updateWorkStatus(Long studentId, String workId, String fileAddress) {
        lambdaUpdate().eq(StudentWorkRelation::getStudentId, studentId)
                .eq(StudentWorkRelation::getWorkId, workId)
                .set(StudentWorkRelation::getIsComplete, WorkCompleteStatus.COMPLETE.getStatus())
                .set(StudentWorkRelation::getFileAddr, fileAddress).update();
    }

    /**
     * 删除学生作业关系
     * @param workId 作业id
     */
    @Override
    public void deleteStudentWorkRelationByWorkId(String workId) {
        lambdaUpdate().eq(StudentWorkRelation::getWorkId, workId).remove();
    }

    /**
     * 获取已完成的学生作业关系
     * @param workId 作业id
     * @return
     */
    @Override
    public List<StudentWorkRelation> getCompleteStudentWorkRelationByWorkId(String workId) {
        List<StudentWorkRelation> result = lambdaQuery().eq(StudentWorkRelation::getWorkId, workId)
                .eq(StudentWorkRelation::getIsComplete, WorkCompleteStatus.COMPLETE.getStatus()).list();
        return result;
    }
}
